/**
 * 
 */
package com.toncen.samepms.common;

import ognl.Ognl;
import ognl.OgnlContext;

import com.toncen.samepms.basic.domain.User;
import com.toncen.samepms.system.domain.Advice;
import com.opensymphony.xwork2.ActionContext;
import com.sky.common.ognl.OgnlUtils;
import com.sky.common.string.StringUtils;
import com.sky.simple.engine.AbstractWorkflowAssigner;
import com.sky.simple.engine.IWorkflowParticipator;
import com.sky.simple.engine.IWorkflowProcessable;
import com.sky.simple.engine.IWorkflowRuntimeContext;
import com.sky.simple.engine.SimpleWorkflow;
import com.sky.simple.model.Activity;
import com.sky.simple.model.Transition;
import com.sky.simple.model.WorkflowProcess;

/**
 * 默认指派器
 * 
 * @author tch
 * 
 */
public class DefaultAssigner extends AbstractWorkflowAssigner {
	/**
	 * 
	 */
	private static final long serialVersionUID = -726947654091111982L;

	/**
	 * 参与者表达式参数
	 */
	private static final String PARAMETER_TARGET = "target";

	/**
	 * 
	 */
	public DefaultAssigner() {
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sky.simple.engine.AbstractWorkflowAssigner#getParticipator(com.sky.simple.engine.IWorkflowRuntimeContext,
	 *      com.sky.simple.engine.IWorkflowProcessable,
	 *      com.sky.simple.model.Transition, com.sky.simple.model.Activity)
	 */
	@Override
	public IWorkflowParticipator getParticipator(IWorkflowRuntimeContext context, IWorkflowProcessable business, Transition transition, Activity activity)
			throws Exception {
		String target = getAssigner().getParameterMap().get(PARAMETER_TARGET);
		if (!StringUtils.isEmpty(target)) {
			User user = new User();
			if (ActionContext.getContext() != null && ActionContext.getContext().getSession().get(ComitConstant.USER_SESSION_ATTRIBUTE) != null) {
				user = (User) ActionContext.getContext().getSession().get(ComitConstant.USER_SESSION_ATTRIBUTE);
			}
			OgnlContext ognlContext = (OgnlContext) Ognl.createDefaultContext(null);
			ognlContext.put(Advice.PARAMETER_USER, user);
			Object root = business;
			Object participator = OgnlUtils.getValue(target, ognlContext, root);
			WorkflowProcess process = SimpleWorkflow.getProcess(context.getProcessId());
			StringBuffer info = new StringBuffer();
			info.append("流程【" + process.getName() + "(" + process.getId() + ")】");
			if (context != null) {
				info.append("上下文环境【" + context.getClass().getSimpleName() + "】");
			}
			if (business != null) {
				info.append("业务流程对象【" + business.getClass().getSimpleName() + "】");
			}
			if (activity != null) {
				info.append("当前活动结点【" + activity.getName() + "(" + activity.getId() + ")】");
			}
			info.append("参与者表达式【" + target + "】");
			if (participator == null) {
				throw new Exception(info + "获取值为空，请与系统管理员联系！");
			}
			if (!(participator instanceof IWorkflowParticipator)) {
				throw new Exception(info + "获取值[" + participator.getClass().getName() + "]不为[" + IWorkflowParticipator.class.getName() + "]类型，请与系统管理员联系！");
			} else {
				return (IWorkflowParticipator) participator;
			}
		}
		return null;
	}

}
